import { getDeviceParameterDb } from '../lib/device-parameter-database';

interface DeviceInfo {
  classification_code: string;
  classification_name: string;
  specialty: string;
}

// 根据设备分类和专业确定合适的表映射
function getTableMappingForDevice(device: DeviceInfo): {
  naming_table: string;
  position_table: string;
  base_params_table: string;
} {
  // 基本映射规则
  let naming_table = 'table_c2';  // 默认命名表
  let position_table = 'table_c6'; // 默认位置表
  let base_params_table = 'table_c494'; // 默认基础参数表

  // 根据设备分类调整位置表
  if (device.classification_name.includes('罐') || 
      device.classification_name.includes('泵') || 
      device.classification_name.includes('鹤管')) {
    position_table = 'table_c4'; // 设备位置表
  }

  // 根据设备分类调整基础参数表
  if (device.classification_name.includes('泵')) {
    // 根据泵的类型选择不同的参数表
    if (device.classification_name.includes('离心') || 
        device.classification_name.includes('消防') ||
        device.classification_name.includes('冷却')) {
      base_params_table = 'table_c18'; // 离心泵基础参数表
    } else if (device.classification_name.includes('提升') ||
               device.classification_name.includes('污水') ||
               device.classification_name.includes('卸车')) {
      base_params_table = 'table_c27'; // 其他泵类参数表
    } else if (device.classification_name.includes('滑片')) {
      base_params_table = 'table_c30'; // 滑片泵参数表
    } else if (device.classification_name.includes('潜油')) {
      base_params_table = 'table_c36'; // 潜油泵参数表
    }
  } else if (device.classification_name.includes('罐')) {
    base_params_table = 'table_c81'; // 储罐基础参数表
  } else if (device.classification_name.includes('鹤管')) {
    base_params_table = 'table_c109_1'; // 鹤管基础参数表
  } else if (device.classification_name.includes('过滤')) {
    base_params_table = 'table_c183'; // 过滤器基础参数表
  } else if (device.classification_name.includes('阻火')) {
    base_params_table = 'table_c173'; // 阻火器基础参数表
  } else if (device.classification_name.includes('呼吸')) {
    base_params_table = 'table_c174'; // 呼吸阀基础参数表
  } else if (device.classification_name.includes('软管')) {
    base_params_table = 'table_c179'; // 软管基础参数表
  } else if (device.classification_name.includes('补偿')) {
    base_params_table = 'table_c178'; // 波纹管基础参数表
  } else if (device.classification_name.includes('球阀')) {
    base_params_table = 'table_c202'; // 球阀基础参数表
  } else if (device.classification_name.includes('闸阀')) {
    base_params_table = 'table_c203'; // 闸阀基础参数表
  } else if (device.classification_name.includes('蝶阀')) {
    base_params_table = 'table_c204'; // 蝶阀基础参数表
  } else if (device.classification_name.includes('止回阀')) {
    base_params_table = 'table_c207'; // 止回阀基础参数表
  } else if (device.classification_name.includes('泄压阀')) {
    base_params_table = 'table_c208'; // 泄压阀基础参数表
  } else if (device.classification_name.includes('管线') || device.classification_name.includes('管道')) {
    base_params_table = 'table_c150'; // 管线基础参数表
  } else if (device.classification_name.includes('橇') || device.classification_name.includes('装置')) {
    base_params_table = 'table_c14'; // 橇装置基础参数表
  }

  return { naming_table, position_table, base_params_table };
}

async function addMissingDeviceMappings() {
  try {
    const db = getDeviceParameterDb();
    const deviceParameterDb = db as any;

    console.log('开始添加缺失的设备类型映射...');

    // 获取所有缺少映射的设备
    const missingMappingsQuery = `
      SELECT DISTINCT d.classification_code, d.classification_name, d.specialty
      FROM devices d 
      LEFT JOIN device_type_mappings m ON d.classification_code = m.classification_code 
      WHERE m.classification_code IS NULL
    `;

    const devicesWithoutMappings = deviceParameterDb.db.prepare(missingMappingsQuery).all();
    console.log(`找到 ${devicesWithoutMappings.length} 个缺少映射的设备分类`);

    // 为每个缺少映射的设备创建映射
    const insertMappingStmt = deviceParameterDb.db.prepare(`
      INSERT INTO device_type_mappings (
        classification_code, 
        classification_name, 
        specialty, 
        naming_table, 
        position_table, 
        base_params_table,
        created_at,
        updated_at
      ) VALUES (?, ?, ?, ?, ?, ?, datetime('now'), datetime('now'))
    `);

    let addedCount = 0;
    
    for (const device of devicesWithoutMappings) {
      const mapping = getTableMappingForDevice(device);
      
      try {
        insertMappingStmt.run(
          device.classification_code,
          device.classification_name,
          device.specialty,
          mapping.naming_table,
          mapping.position_table,
          mapping.base_params_table
        );
        
        console.log(`添加映射: ${device.classification_code} -> ${device.classification_name}`);
        console.log(`  命名表: ${mapping.naming_table}`);
        console.log(`  位置表: ${mapping.position_table}`);
        console.log(`  参数表: ${mapping.base_params_table}`);
        
        addedCount++;
      } catch (error) {
        console.error(`添加映射失败 ${device.classification_code}:`, error);
      }
    }

    console.log(`\n成功添加了 ${addedCount} 个设备类型映射`);
    console.log('映射添加完成！');

  } catch (error) {
    console.error('添加设备映射失败:', error);
    process.exit(1);
  }
}

// 如果直接运行此脚本
if (require.main === module) {
  addMissingDeviceMappings();
}